<?php
class Support extends CWidget
{
	public $title='Hỗ trợ trực tuyến';
	public $_wID = NULL;
	public function init()
	{
		parent::init();
	}
	/* public function renderContent()
	{
		$params = Widgets::model()->findByPk($this->_wID);
		$data = @unserialize($params->params);
		$this->render('default', array('data'=>$data));
	} */
	public function run()
	{
		$params = FrontendWidgetsModel::model()->findByPk($this->_wID);
		$data = @unserialize($params->params);
		$this->render('default', array(
				'data'=>$data,
				'title'=>$this->title
		));
	}
	public static function buildParams($params="")
	{
		$data = @unserialize($params);
		//echo '<pre>';print_r($data);
		
		?>
		<script>
		$(document).ready(function(){
			var i = $('.inputs input').size() + 2;
			$('#add').click(function() {
				//var html = '<select name="Support['+i+'][type]"><option value="Yahoo">Yahoo</option><option value="Skype">Skype</option><option value="Mobile">Mobile</option><option value="Phone">Phone</option></select>'
				var html = '<div id="f'+i+'">'
					+'<fieldset><legend>Hỗ trợ '+i+'</legend>'
					+'<label>Tên:</label><input type="text" name="Support['+i+'][name]" value="" />'
					+'<label>Số điện thoại:</label><input type="text" name="Support['+i+'][phone]" value="" />'
					+'<label>Yahoo:</label><input type="text" name="Support['+i+'][yahoo]" value="" />'
					+'&nbsp;<a onclick="$(\'#f'+i+'\').remove();" href="#">Xóa</a>'
					+'</fieldset>'
					+'</div>';
				$(html).fadeIn('slow').appendTo('.inputs');
				i++;
			});
		})
		</script>
		<div class="inputs">
		<?php if($data){
			foreach ($data as $key => $value){
				?>
				<div id="f<?php echo $key;?>">
				<fieldset><legend>Hỗ trợ <?php echo $key;?></legend>
				<label>Tên:</label><input type="text" name="Support[1][name]" value="<?php echo $value['name'];?>" />
				<label>Số điện thoại:</label><input type="text" name="Support[1][phone]" value="<?php echo $value['phone'];?>" />
				<label>Yahoo:</label><input type="text" name="Support[1][yahoo]" value="<?php echo $value['yahoo'];?>" />
				&nbsp;<a onclick="$('#f<?php echo $key;?>').remove();" href="#">Xóa</a>
					
				</fieldset>
				
				</div>
				<?php
			}
			
		}else{
			?>
			<div id="f1">
			<fieldset><legend>Hỗ trợ 1</legend>
			<label>Tên:</label><input type="text" name="Support[1][name]" value="" />
			<label>Số điện thoại:</label><input type="text" name="Support[1][phone]" value="" />
			<label>Yahoo:</label><input type="text" name="Support[1][yahoo]" value="" />
			&nbsp;<a onclick="$('#f1').remove();" href="#">Xóa</a>
				
			</fieldset>
			
			</div>
			<?php
		}
		?>
		</div>
		<input type="hidden" name="widget" value="Support" />
		<a id="add" href="#">Thêm hỗ trợ</a>
		<?php
	}
	public function getParams($post)
	{
		return  serialize($post['Support']);
	}
	private static function getOption($name="",$res="")
	{
		$option = array('Yahoo','Skype','Mobile','Phone');
		$html = "<select name='{$name}'>";
		foreach ($option as $value){
			if($res!='' && $res==$value){
				$selected = "selected";
			}else{
				$selected = "";
			}
			$html .="<option value='{$value}' {$selected}>{$value}</option>";
		}
		return $html."</select>";
	}
}